class UserCard extends HTMLElement {
  constructor() {
    super();

    const template = document.createElement('template');

    template.innerHTML = `
    <style>
      :host {
        display: flex;
        align-items: center;
        width: 450px;
        height: 180px;
        background-color: #d4d4d4;
        border: 1px solid #d5d5d5;
        box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.1);
        border-radius: 3px;
        overflow: hidden;
        box-sizing: border-box;
        font-family: 'Poppins', sans-serif;
      }

      .image {
        flex: 0 0 auto;
        width: 160px;
        height: 160px;
        vertical-align: middle;
        border-radius: 5px;
        padding: 10px;
      }

      .container {
        box-sizing: border-box;
        padding: 20px;
        height: 160px;
      }

      .container>.name {
        font-size: 20px;
        font-weight: 600;
        line-height: 1;
        margin: 0;
        margin-bottom: 5px;
      }

      .container>.email {
        font-size: 12px;
        opacity: 0.75;
        line-height: 1;
        margin: 0;
        margin-bottom: 15px;
      }

      .container>.button {
        padding: 10px 25px;
        font-size: 12px;
        border-radius: 5px;
        text-transform: uppercase;
      }
    </style>

    <img src="https://semantic-ui.com/images/avatar2/large/kristy.png" class="image">
    <div class="container">
      <p class="name">User Name</p>
      <p class="email">yourmail@some-email.com</p>
      <p>
        <slot name="default">默认插槽</slot>
      </p>
      <button class="button">关注</button>
    </div>
    `;

    let shadow = this.attachShadow({
      mode: 'closed'
    });

    let content = template.content.cloneNode(true);
    content.querySelector('img').setAttribute('src', this.getAttribute('image'));
    content.querySelector('.container>.name').innerText = this.getAttribute('name');
    content.querySelector('.container>.email').innerText = this.getAttribute('email');
    this.$button = content.querySelector('.container>.button');

    // 监听FOLLOW按钮的点击事件
    this.$button.addEventListener('click', () => {
      this.btnClick();
    });

    shadow.appendChild(content);
  }

  // 生命周期函数
  connectedCallback() {
    console.log('当 custom element首次被插入文档DOM时，被调用。');
  }

  disconnectedCallback() {
    console.log('当 custom element从文档DOM中删除时，被调用。');
  }

  adoptedCallback() {
    console.log('当 custom element被移动到新的文档时，被调用。');
  }

  attributeChangedCallback(name, oldVal, newVal) {
    console.log('当 custom element增加、删除、修改自身属性时，被调用。');
    console.log(name, oldVal, newVal);
  }
}

window.customElements.define('user-card', UserCard);